<?php

// @filename   mmember.php

class Mmember extends Model {

    function Mmember() {
        parent::Model();
    }
    
    function get_member_list($type='',$offset=0,$limit=10){
    	if($type != ''){
    		$this->db->where(array('utype'=>$type));
    	}
    	$this->db->where('state','1');
    	$this->db->limit($limit,$offset);
    	$this->db->order_by('state desc, uid desc, reg_time desc');
    	$query = $this->db->get('members');
    	return $query->result_array();
    }
    
    function get_member_count($type=''){
    	if ($type){
    		$this->db->where(array('utype'=>$type));
    	}
    	$this->db->where('state','1');
    	return $this->db->count_all_results('members');
    }
    
    function get_search_result($utype,$method,$word,$offset=0,$limit=12){
    	if ($utype != 'all'){
    		$this->db->where(array('utype'=>$utype));
    	}
    	$this->db->where('state','1');
    	$this->db->like($method,$word);
    	$this->db->limit($limit,$offset);
    	$this->db->order_by('state desc, uid desc, reg_time desc');
    	$query = $this->db->get('members');
    	return $query->result_array();
    }
    
    function get_search_count($utype,$method,$word){
    	if ($utype != 'all'){
    		$this->db->where(array('utype'=>$utype));
    	}
    	$this->db->where('state','1');
    	$this->db->like($method,$word);
    	return $this->db->count_all_results('members');
    }
    
    /**
     * 更新用户数据
     * @param $uid
     * @param $data
     * @return TRUE/FALSE
     */
    function update_member_data($uid,$data){
    	return $this->db->update('members',$data,array('uid'=>$uid));
    }

    /**
     * 获取一个会员的详细信息
     * @param <int> $uid
     * @return <array/FALSE>
     */
    function get_member_info($uid){
        $query = $this->db->get_where('members',array('uid'=>$uid));
        return $query->row_array();
    }
    
    function deal_add_member($type,$ids,$pwd,$discount_info){
    	$disc_type = $discount_info['disc_type'];
    	$discount = $discount_info['discount'];
    	$sql = "INSERT INTO ".$this->db->dbprefix."members(uid,username,password,utype,reg_time,reg_ip,disc_type,discount) VALUES";
    	$arr = array();
    	$date = date('Y-m-d H:i:s');
    	$ip = $this->input->ip_address();
    	foreach ($ids as $id){
    		$username = $type.str_pad($id,6,'0',STR_PAD_LEFT);
    		$arr[] = "(NULL,'$username','$pwd','$type','$date','$ip','$disc_type','$discount')";
    	}
    	$sql .= implode(',',$arr);
    	return $this->db->query($sql);
    }
    
    function insert_new_user($values){
    	return $this->db->insert('members',$values);
    }
    
    function new_member_ids($count){
    	$sql = "INSERT INTO ".$this->db->dbprefix."member_ids VALUES";
    	$arr = array();
    	for ($i=0;$i<$count;$i++){
    		$arr[] = "(NULL,0)";
    	}
    	$sql .= implode(',',$arr);
    	return $this->db->query($sql);
    }
    
    function get_valid_member_id($count){
    	$this->db->order_by('uid','asc');
    	$query = $this->db->get_where('member_ids',array('state'=>0),$count);
    	$res = $query->result_array();
    	$ids = array();
    	foreach ($res as $arr){
    		$ids[] = $arr['uid'];
    	}
    	return $ids;
    }
    
    function update_member_id_state($id){
    	$sql = "UPDATE ".$this->db->dbprefix."member_ids SET state=1 WHERE uid";
    	if (is_array($id)) {
    		$sql .= " IN(".implode(',',$id).")";
    	} else {
    		$sql .= "=".$id;
    	}
    	return $this->db->query($sql);
    }
    
    /**
     * 检查登录用户名和密码是否正确
     * @param $username
     * @param $password
     * @return int 1/2/99
     */
    function check_user_valid($username,$password){
    	$query = $this->db->get_where('members',array('username'=>$username));
    	$row = $query->row();
    	if ($row){
    		if ($row->password == $password){
    			return 99;
    		} else {
    			return 2;
    		}
    	} else {
    		return 1;
    	}
    }
    
    function check_user_exist($username){
    	$query = $this->db->get_where('members',array('username'=>$username));
    	$row = $query->row();
    	if ($row){
    		return true;
    	} else {
    		return false;
    	}
    }
    
    /**
     * 获取用户信息
     * @param $key
     * @param $val
     * @return array/FALSE
     */
    function get_user_info($key,$val){
    	$query = $this->db->get_where('members',array($key=>$val));
    	return $query->row_array();
    }
    
    /**
     * 根据用户类型获取折扣信息
     * @param $utype
     * @return array/FALSE
     */
    function get_discount_info($utype){
    	$query = $this->db->get_where('member_types',array('en_name'=>$utype));
    	return $query->row_array();
    }
    
    function insert_new_discount($values){
    	return $this->db->insert('discount',$values);
    }

    /**
     * 获取用户总的提成信息
     * @param <type> $username
     * @return <type>
     */
    function get_discount($username){
        $sql = "SELECT SUM(price) AS price, SUM(discount) AS discount FROM ".$this->db->dbprefix."discount WHERE username='$username'";
        $query = $this->db->query($sql);
        return $query->row_array();
    }
    
    /**
     * 找回用户密码
     * @param $user
     * @param $email
     * @return unknown_type
     */
    function get_pwd_back($user,$email){
    	$this->db->where('username',$user);
    	$this->db->or_where('realname',$user);
    	$query = $this->db->get('members');
    	$row = $query->row();
    	if (!$row){
    		return '1';
    	} else {
    		if ($row->email == $email){
    			return $row->realname.'|||'.$row->username.'|||'.$row->password;
    		} else {
    			return '2';
    		}
    	}
    }
    
}
?>